<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['params']=array();
$merchid=plugin('jy_merch')->checklogin();
$return['status']=array(
		'0'=>'待付款_NO pay',
		'1'=>'待发货_NO send',
		'2'=>'待收货_NO get',
		'3'=>'已完成_Payment complete',
		'4'=>'已取消_exchange',
		'5'=>'申请退款',
		'6'=>'已退款',
		'7'=>'异常报备',
		'10'=>'拼单中',
		'11'=>'待售后接单',
		'12'=>'售后已接单',
		'13'=>'售后上门',
		'14'=>'售后异常',
		'15'=>'待售后',
		'16'=>'售后完成',
		'21'=>'部分发货',
		'-1'=>'已关闭',
);
$return['send_status']=$this->send_status;
$return['status2']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['paytype']['offpay']='线下收款';
$return['paytype']['offsao']='线下扫码';
$return['ispay']=array('0'=>'未支付_No pay','1'=>'已支付_Is pay');
$install = check_account_permission('jy_peisong');
if($install){
	model('jy_peisong/order')->order_checktime();
}
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'deleted'=>0,'plugin_name <>'=>'jy_mfuwu');
	$return['params']=array('deleted'=>0);
    $orderby='createtime desc';
	$total='';
	if(!empty($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(isset($_RQ['send_status'])){
		$params['send_status']=intval($_RQ['send_status']);
		$return['params']['send_status']=$_RQ['send_status'];
	}
	if(isset($_RQ['status']) && $_RQ['status'] !='100'){
		if($_RQ['status']==15){
			$params['status']=array(11,12,13,14,15,16);
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']=100;
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if($_RQ['postdata']){
		$postdata=get_postdata();
		
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(trim($postdata['keyword'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=trim($postdata['keyword']);
		
	}
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id'],'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),array('id','goodsid','orderid','optionid','price','total','total_price','goodssn','productsn','fee_desc'));
		$v['goods']=array();
		$v['commission_money']=0;
		$v['merch_money']=0;
		foreach($ordergoods as $gv){
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','image','title','merchid'));
			$goodsinfo['optionid']=$gv['optionid'];
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			$goodsinfo['image']=tomedia($goodsinfo['image']);
			$goodsinfo['total_price']=$gv['total_price'];
			$v['merch_money'] +=$goodsinfo['total_price'];
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			$v['goods'][]=$goodsinfo;
			//计算佣金
			if($this->plugin_exists('jy_commission')){
					$commission_money=model('jy_commission/commission')->get_goodsfee($gv['goodsid'],$gv['orderid'],$gv['id']);
					$v['commission_money'] +=$commission_money;
			}
			$total_price=$gv['price']*$gv['total'];
			if($total_price!=$gv['total_price']){
				pdo_update('jy_weishop_order_goods',array('total_price'=>$total_price),array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['id']));
			}
		}
		$v['merch_money']=price($v['merch_money']);
		$v['commission_money']=price($v['commission_money']);
		$v['sendtypetitle']=$return['sendtype'][$v['sendtype']];
		if($v['sendtype']==1 && $v['is_peisong']==1){
			$v['sendtypetitle']='配送上门';
		}
		if($v['send_status'] && $v['status']==1){
			$v['statustitle']=$this->send_status[$v['send_status']];
		}else{
			$v['statustitle']=$this->order_status[$v['status']];
		}
		$v['buytypetitle']=$this->buy_types[$v['buytype']];
		$v['member']=model('jy_merch/member')->member_show($v['mid'],$v['merchid']);
		if($_SESSION['os']!='windows'){
			$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		}
		$self=pdo_get('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$v['id']));
		$v['selfnumber']=$self['number'];
		$v['selfnumber2']=substr($v['selfnumber'],-4);
		$v['merchorder']=model('jy_merch/commission')->get_choufee($v['id'],true,$merchid);
		$v['merchorder_commission_money']=$v['merchorder']['commission_money']?:0;
		if($v['merchorder']['status']){
			$v['merchorder_statustitle']='可结算';
		}else{
			$v['merchorder_statustitle']=$v['merchorder']['commission_money']?'不可结算':'未成交';
		}
		$merch_order=pdo_get('jy_merch_order',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$v['id']));
		$v['sys_peiprice']=$merch_order['sys_peiprice'];
		$v['total']=$v['goods'][0]['total'];
		$v['title']=$v['goods'][0]['title'];
		$v['image']=$v['goods'][0]['image'];
		$v['unit']=$v['goods'][0]['unit'];
		if($v['buytype']==4){
			if($v['dispatchprice']>0){
				$v['dispatchpricetitle']='客户已付运费';
			}else{
				$api_data=json_decode($v['api_data'],true);
				if($v['sys_peiprice']<=0 && $api_data['isbaoyoufee']>0){
					pdo_update('jy_merch_order',array('sys_peiprice'=>$api_data['isbaoyoufee']),array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$v['id']));
				}
				$v['sys_peiprice']=$api_data['isbaoyoufee'];
				$v['dispatchpricetitle']=$merch_order['isyoufee']?'商家已付运费':'商家需付运费';
				if($v['sys_peiprice']>0 && !$merch_order['isyoufee']){
					$v['needyoufee']=1;
				}
			}
		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','openvoice'=>true,'openvoice_action'=>'jy_weishop/order.list/getnew/merchid='.$merchid,'edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('type'=>'img_string','title'=>'商品名称/数量_Goods','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'数量：_num：','name'=>'total','unit'=>'unit')
				)
			),
			array('type'=>'img_string','title'=>'订单金额',
			'data'=>array(
				array('title'=>'订单金额：￥','name'=>'price'),
				array('title'=>'商户金额：','name'=>'merch_money')
				)
			),
			array('type'=>'img_string','title'=>'抽成情况',
			'data'=>array(
				array('title'=>'抽成金额：￥','name'=>'merchorder_commission_money'),
				array('title'=>'分销佣金：','name'=>'commission_money'),
				array('title'=>'状态：','name'=>'merchorder_statustitle')
				)
			),
			array('type'=>'string_more','title'=>'支付/状态_Is pay',
			'data'=>array(
				array('title'=>'是否支付_Is pay?','name'=>'ispay','type'=>'radio','options'=>'ispay'),
				array('title'=>'支付方式_Pay type','name'=>'paytype','type'=>'radio','options'=>'paytype'),
				array('title'=>'类型_Buy type','name'=>'buytype','type'=>'radio','options'=>'buytype')
				)
			),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),		
		),
		'action'=>array(	
			array('type'=>'href','action'=>'/merch.order_detail/','title'=>'订单详情'),
			array('op'=>'fa','title'=>'发货_Ship','type'=>'modal','show'=>array('status'=>1,'sendtype'=>1,'send_status'=>array(0,11))),
			array('op'=>'fa','title'=>'自提_Ship','type'=>'modal','show'=>array('status'=>1,'sendtype'=>2,'send_status'=>array(0,11))),
			array('op'=>'fa','title'=>'虚拟发货_Ship','type'=>'modal','show'=>array('status'=>1,'sendtype'=>3,'send_status'=>array(0,11))),
			array('op'=>'print','title'=>'订单打印','type'=>'post'),
			array('op'=>'print4','title'=>'结账单打印','type'=>'post'),
			array('op'=>'print5','title'=>'付款小票打印','type'=>'post'),
			array('type'=>'post','op'=>'shou','title'=>'确认收货','show'=>array('status'=>2)),	
		)),
		'search'=>array(
			array('name'=>'ordersn','width'=>15,'title'=>'订单号','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'订单状态','type'=>'radio','options'=>'status'),
			array('name'=>'buytype','width'=>15,'title'=>'订单类型','type'=>'radio','options'=>'buytype'),
			array('name'=>'sendtype','width'=>15,'title'=>'配送方式','type'=>'radio','options'=>'sendtype'),
		)
	);
	if($params['status']==1){
		$htmls['list']['list'][]=array('name'=>'send_status','title'=>'配送状态','type'=>'radio','options'=>'send_status');
	}else{
		$htmls['list']['list'][]=array('name'=>'status','title'=>'订单状态_status','type'=>'radio','options'=>'status');
	}
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('merch/order_index');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
	}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'plugin_name <>'=>'jy_mfuwu');
	$params['deleted']=intval($_RQ['deleted']);
	if(!empty($_RQ['ordersn'])){
        $params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(isset($_RQ['status']) && $_RQ['status'] !='100'){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']=100;
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	$order=pdo_getall('jy_weishop_order',$params);
	if(count($order)>5000){
		exi('订单数量大于5000，请修改搜索条件！','error');
	}
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	$ordergoods=array();
	foreach($order as &$v){
		$member=model('jy_merch/member')->member_show($v['mid'],$v['merchid']);
		$v['nickname']=$member['nickname'];		
		$v['usermobile']=$member['mobile'];	
		$v['address']=json_decode($v['address'],true);
		if($v['address'] && ($v['address']['name'] || $v['address']['mobile'])){	
			$address=$v['address'];
		}else{
			$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['addressid']));
		}
		$v['address']=model('core/member')->get_address_str($address);
		$v['address']=str_replace(',','，',$v['address']);
		$v['address']=str_replace("\n","，",$v['address']);
		$v['remark']=str_replace(',','，',$v['remark']);
		$v['remark']=str_replace("\n","，",$v['remark']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname']?:$address['name'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['paytime']=$v['paytime']?date('Y-m-d H:i:s',$v['paytime']):'';
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$v['diyformdata']=json_decode($v['diyformdata'],true);
		$v['data2']='';
		if(is_array($v['diyformdata'])){
			foreach($v['diyformdata'] as $dk=>&$diyv){
					$v['data2'] .='【'.$diyv['title']."】：";
					if(is_array($diyv['data']) && $diyv['type']!=7){
						$diyv['data']=$diyv['data'][0].' '.$diyv['data'][1].' '.$diyv['data'][2].' '.$diyv['data'][3];
					}
					if($diyv['type']==7){
						$diyv['data']=$diyv['data']['province'].' '.$diyv['data']['city'].' '.$diyv['data']['district'];
					}
					$v['data2'] .=$diyv['data'];
			}
		}
		$invoice=pdo_get('jy_weishop_order_invoice',array('orderid'=>$v['id']));
		if($invoice){
			$type=$invoice['type']==2?'增值税专用发票':'电子普通发票';
			$v['invoice']='['.$type.'] 抬头：'.$invoice['name'].' ，税号：'.$invoice['code'].' ，开户行：'.$invoice['bank'].' ，银行账号：'.$invoice['bankcode'].' ，邮箱：'.$invoice['email'];
		}
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),array('goodsid','optionid','total'));
		foreach($goods as $k=>$gv){
			$info=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid']),array('api_id','api_pluginname','title'));
			if($info['api_pluginname']=='jy_supplier'){
                $info['supplier_id']=$info['api_id'];
            }
            $goodstitle=$info['title'];
			//商品规格
			if($gv['optionid']){
				$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs','goodssn','productsn'));
				$specs=$options['specs'];
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$selespename=implode("，",$specs);
			}else{
				$options=array();
				$selespename='无规格';
			}
			$num=$k+1;
			if($k){
				$v=array();
			}
			$v['goodstitle']='【商品'.$num.'】'.$goodstitle;
			$v['selespename']=$selespename;
			$v['goodssn']=$options['goodssn']?:$v['goodssn'];
			$v['productsn']=$options['productsn']?:$v['productsn'];
			$v['total']=$gv['total'];
			if($this->plugin_exists('jy_supplier') && $info['supplier_id']){
				$v['supplier']=pdo_getcolumn('jy_supplier_user',array('id'=>$info['supplier_id']),'merchname');
			}
            $v['costprice']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid']),'costprice');
			$ordergoods[]=$v;
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'goodstitle'=>'商品名称',
		'selespename'=>'规格',
		'goodssn'=>'编码',
		'productsn'=>'国家商品条码',
		'total'=>'数量',
		'nickname'=>'买家昵称',
		'usermobile'=>'买家电话',
		'price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'支付状态',
		'paytype'=>'支付方式',
		'paytime'=>'支付时间',
		'realname'=>'收货人姓名',
		'address'=>'收货地址',
		'mobile'=>'收货人电话',
		'invoice'=>'发票信息',
        'costprice'=>'成本价',
        'supplier'=>'供货商',
		'remark'=>'备注',
		'data2'=>'表单内容',
		'dispatchprice'=>'运费',
		'level_de_price'=>'会员折扣',
		'delcredit1'=>'积分抵扣',
		'enough_de_price'=>'满额立减',
		'coupon_de_price'=>'优惠券优惠',
		'luck_de_price'=>'运势优惠',
		'isdiscountprice'=>'促销优惠',
		'out_upfee'=>'打包费',
	);
	cfc('tools')->export2excel($header,$ordergoods,'订单列表');
}
if($_RQ['op']=='fa'){
	$install = check_account_permission('jy_peisong');
	if(!$install){
		exi('请联系管理员开通《配送员》插件！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['sendtype']==1){
			if($post['buytype']==4){
				if(!$post['merchfa']){
					exi('请选择发货方式！','error');
				}
				if($post['merchfa']==1){
					model('jy_weishop/order')->order_send($id);
					exi('订单配送处理成功！','','/merch.order_list/list/status=1');
				}elseif($post['merchfa']==2){
					pdo_update('jy_weishop_order',array('send_status'=>12),array('id'=>$id));
					pdo_update('jy_weishop_order_goods',array('send_status'=>12),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
				}
			}else{
				if($post['express_type']==0){
					if(!$post['morefa'] && !$post['fatype'] && (!$post['express'] || !$post['expresssn'])){
						exi('请填写快递信息','error');
					}
					if(!$post['morefa'] && $post['fatype'] && !$post['content']){
						exi('请填写物流信息','error');
					}
					model('jy_weishop/order')->order_send($id,$post);
				}elseif($post['express_type']==1){//平台配送
					if(!$post['paisong_id'] && $post['need_setpeisong']){
						exi('请选择配送员！','error');
					}
					if($post['paisong_id']){
						$post['order_id']=$id;
						model('jy_peisong/order')->order_get($post);
					}else{
						$post['send_status']=12;
						cfc('jinyun_socket',$_SESSION['uniacid'])->send2room('刷新配送订单！','peisong_order');
						model('jy_weishop/order')->order_send($id,$post);
					}			
					exi('订单配送处理成功！','','/merch.order_list/list/status=1');
				}elseif($post['express_type']==2){//商家自己配送
					model('jy_weishop/order')->order_send($id);
					exi('订单配送处理成功！','','/merch.order_list/list/status=1');
				}elseif($post['express_type']==3){//达达配送
					$res=model('jy_weishop/order/dada_api')->dada_send($post);
					if($res['state']==1){
						$post['opensend_type']=1;
						model('jy_weishop/order')->order_send($id,$post);
					}else{
						exi($res['msg'],'error');
					}
				}
			}			
		}elseif($post['sendtype']==3){
			model('jy_weishop/order')->order_send_virtual($id,$post['content']);
		}elseif($post['sendtype']==2){
			model('jy_merch/verify')->self_verify($post['verifycode']);
		}
		exi('已发货！','','/merch.order_list/list/status=1');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order',array('id'=>$id),array('id','is_peisong','sendtype','storeid','merchid','address','addressid','ordersn','remark','price','isdish','buytype'));	
	$params=array('merchid'=>$merchid,'orderid'=>$id,'uniacid'=>$_SESSION['uniacid']);
	$ordergoods=pdo_getall('jy_weishop_order_goods',$params,array('status','optionid','total','goodsid','istaocan','api_pluginname','api_id','id'));
	$item['canfa']=1;
	foreach($ordergoods as &$gv){
		$gv['sendtype']=$item['sendtype'];
		$gv['nodel']=1;
		$gv['refund_id']=pdo_getcolumn('jy_weishop_order_refund',array('ordersn'=>$item['ordersn'],'ordergoodsid'=>$gv['id']),'id');
		if($gv['refund_id']){
			$gv['noedit']=1;
		}
		$gv['title']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['goodsid']),'title');
		//商品规格
		if($gv['optionid']){
			$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs'));
			if($gv['istaocan']){
				$gv['selespename']=$options['title'];
			}else{
				$specs=$options['specs'];
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$gv['selespename']=implode("，",$specs);
			}
		}else{
			$gv['selespename']='无';
		}
		$isfa=pdo_getcolumn('jy_weishop_order_express',array('uniacid'=>$_SESSION['uniacid'],'ordergoodsid like'=>'%'.$gv['id'].'%'),'id');
		if(!$isfa){
			$gv['canfa']=1;
		}else{
			$gv['canfa']=0;
		}
	}
	$item['goods']=$ordergoods;
	$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('ismake','mypeisong','lat_lng'));
	if($item['sendtype']==1 || $item['sendtype']==4){
		$htmls=array(
			'type'=>'edit',
			'desc'=>'订单发货',
		);
		$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
		$item['citypeisong_isopen']=$citypeisong['isopen'];
		if($merch['mypeisong']){
			$citypeisong['express_type']=2;
		}
		$tip='商品发货处理！';
		$item['citypeisong_express_type']=$citypeisong['express_type']?:0;
		if($citypeisong['isopen']==1 || $item['isdish']){
			$item['express_type']=$citypeisong['express_type']?:1;
			if(!$item['isdish'] && $item['is_peisong']==2){
				$item['express_type']=0;
				$tip='商品订单已打包好,快递发货！';
			}
			if($item['express_type']==1){
				$tip='商品订单已打包好,由平台配送员配送！';
			}elseif($item['express_type']==2){
				$tip='商品订单已打包好,商家自己配送,无需配送员！';
			}elseif($item['express_type']==3){
				$tip='商品订单已打包好,发送给达达配送！(<red>【慎重操作】：确定要呼叫《达达配送员》过来拿货吗?</red>)';
			}
		}else{
			$item['express_type']=0;
			$tip='商品订单已打包好,快递发货！';
		}
		$item['address']=json_decode($item['address'],true);
		if($item['address'] && ($item['address']['address'] || $item['address']['mobile'])){
			$address=$item['address'];
			$addressid=$item['address'];
		}else{
			$address=pdo_get('core_address',array('id'=>$item['addressid']));
			$addressid=$item['addressid'];
		}
		$item['address']=array(
			'address'=>model('core/member')->get_address_str($addressid),
			'mobile'=>$address['mobile'],
			'name'=>$address['name'],
		);
		$item['fatype']=0;
		$item['morefa']=0;
		$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'copy','title'=>'联系人','name'=>'address.name'),
			array('type'=>'copy','title'=>'联系电话','name'=>'address.mobile'),
			array('type'=>'copy','title'=>'收货地址','name'=>'address.address'),
			array('type'=>'switch','title'=>'分批发货？','name'=>'morefa','show'=>array('express_type'=>0,'canfa'=>1)),
			array('type'=>'array','name'=>'goods','title'=>'商品详情','noadd'=>1,'nosplice'=>1,'noedit'=>1,'show'=>array('morefa'=>0),'help'=>'[<red>无需发货</red>的商品]：一般是虚拟商品！',
                'list'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status'),
					array('type'=>'radio','title'=>'是否需要发货','name'=>'canfa','options'=>'canfa'),
				),
				'details'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'radio','title'=>'物流选择','name'=>'fatype','options'=>'fatype'),
					array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('fatype'=>0)),
					array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('fatype'=>0)),
					array('type'=>'string','title'=>'物流内容','name'=>'content','show'=>array('fatype'=>array(1,2))),
				),
            ),
			array('type'=>'array','name'=>'goods','title'=>'商品详情','noadd'=>1,'nosplice'=>1,'editbtn'=>'商品发货','show'=>array('morefa'=>1),
                'list'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status'),
					array('type'=>'radio','title'=>'物流选择','name'=>'fatype','options'=>'fatype'),
				),
				'details'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'radio','title'=>'物流选择','name'=>'fatype','options'=>'fatype'),
					array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('fatype'=>0)),
					array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('fatype'=>0)),
					array('type'=>'string','title'=>'物流内容','name'=>'content','show'=>array('fatype'=>array(1,2))),
				),
            ),
			array('type'=>'radio','title'=>'物流选择','name'=>'fatype','options'=>'fatype','show'=>array('express_type'=>0,'morefa'=>0,'canfa'=>1)),
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('express_type'=>0,'fatype'=>0,'morefa'=>0,'canfa'=>1)),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('express_type'=>0,'fatype'=>0,'morefa'=>0,'canfa'=>1)),
			array('type'=>'string','title'=>'物流内容','name'=>'content','show'=>array('express_type'=>0,'morefa'=>0,'canfa'=>1,'fatype'=>array(1,2))),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定配送员？','show'=>array('express_type'=>1,'canfa'=>1),'help'=>'不指定，则由配送员自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定配送员','name'=>'paisong_id','options'=>'peisongs','option_value'=>'mid','show'=>array('need_setpeisong'=>1,'canfa'=>1),'help'=>'只能指定该门店下面的配送员，或本城市的平台配送员！'),
		);
		if($item['buytype']==4){
			$ismerchfa=intval($_RQ['merchfa']);
			if($ismerchfa){
				$item['merchfa']=$ismerchfa;
				$htmls['edit']=array(
					array('type'=>'tip','title'=>'正在操作通知[平台发货]：让平台发货，平台分配员，由安排发货!'),
				);
			}else{
				$htmls['edit']=array(
					array('type'=>'tip','title'=>'[自行发货]：商家自己发货，不走平台！[平台发货]：让平台发货，平台分配员，由安排发货!'),
					array('type'=>'radio','title'=>'发货方式','name'=>'merchfa','options'=>'merchfa'),
				);
			}
			$return['merchfa']=array(1=>'自行发货',2=>'平台发货');
		}
		$return['express']=model('jy_weishop/model')->express_types();
		$return['fatype']=array('快递配送','其他物流','无需物流');
	}else{
		$item['verifycode']=pdo_getcolumn('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']),'number');
		$htmls=array(
			'type'=>'edit',
			'desc'=>$item['sendtype']==3?'虚拟发货':'物品自提',
		);
		$item['morefa']=0;
		$htmls['edit']=array(
			array('type'=>'switch','title'=>'分批发货？','name'=>'morefa'),
			array('type'=>'array','name'=>'goods','title'=>'商品详情','noadd'=>1,'nosplice'=>1,'noedit'=>1,'show'=>array('morefa'=>0),
                'list'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status'),
				),
				'details'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2)),
					array('type'=>'string','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'=>3),'help'=>'虚拟物品发货，填写发货内容！'),
				),
            ),
			array('type'=>'array','name'=>'goods','title'=>'商品详情','noadd'=>1,'nosplice'=>1,'editbtn'=>'商品发货','show'=>array('morefa'=>1),
                'list'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'radio','title'=>'商品状态','name'=>'status','options'=>'status'),
				),
				'details'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2)),
					array('type'=>'string','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'=>3),'help'=>'虚拟物品发货，填写发货内容！'),
				),
            ),
			array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2,'morefa'=>0)),
			array('type'=>'text','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'=>3,'morefa'=>0),'help'=>'虚拟物品发货，填写发货内容！'),
		);
	}
	$return['canfa']=array('无需发货','需要发货');
	$peisongs=array();
	//多商户用户，选择平台区域配送员
	if($item['merchid']){
		$areas=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>0));
	}else{
		$areas=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>array($item['storeid'],0)));
	}
	foreach($areas as $v){
			$users=pdo_getall('jy_peisong_member',array('uniacid'=>$_SESSION['uniacid'],'group_id'=>$v['id'],'status'=>1,'online >'=>0),array('mid','title'));
			foreach($users as $uv){
					$peisongs[]=$uv;
			}
	}
	$return['peisongs']=$peisongs;
	//if($_SESSION['os']=='mobile'){
	//	$return['htmls']=$this->template_url('merch/order_index');
	//}else{
		$return['htmls']=$htmls;
	//}
	$return['item']=$item;
	exi($return);
}	

if($_RQ['op']=='ispay'){
	$id=intval($_RQ['id']);
	exi('暂不支持线下收款!','error');
	pdo_update('jy_weishop_order',array('ispay'=>1,'status'=>1,'pay_id'=>0,'paytype'=>'offpay','clearstatus'=>1),array('id'=>$id));
	pdo_update('jy_weishop_order_goods',array('clearstatus'=>3),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id,'clearstatus'=>0));
	exi('已确认付款','');
}

if($_RQ['op']=='shou'){
	$id=intval($_RQ['id']);
	exi('暂时不支持，后台手动收货！','error');
	model('jy_weishop/order')->order_receive($id);
	exi('已收货','','/merch.order_list/list/status=3');
}
if($_RQ['op']=='daifa'){
	$id=intval($_RQ['id']);
	pdo_update('jy_weishop_order',array('is_daifa'=>1),array('id'=>$id));
	pdo_update('jy_weishop_order_goods',array('is_daifa'=>1),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
	exi('申请平台代发成功！','','/merch.order_list/list/status=3');
}
if($_RQ['op']=='jiedan'){
	$id=intval($_RQ['id']);
	pdo_update('jy_weishop_order',array('send_status'=>16),array('id'=>$id));
	pdo_update('jy_weishop_order_goods',array('send_status'=>16),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
	exi('接单成功！','','/merch.order_list/list/status=1');
}
if($_RQ['op']=='merchfa'){
	$id=intval($_RQ['id']);
	$merchfa=intval($_RQ['merchfa']);
	if(!$merchfa){
		exi('请选择发货方式！','error');
	}
	if($merchfa==1){
		model('jy_weishop/order')->order_send($id);
		exi('订单配送处理成功！','','/merch.order_list/list/');
	}elseif($merchfa==2){
		pdo_update('jy_weishop_order',array('send_status'=>12),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		pdo_update('jy_weishop_order_goods',array('send_status'=>12),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
	}
	exi('操作完成！','','/merch.order_list/list/');
}
if($_RQ['op']=='deleted'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'deleted'=>1);
	$return['params']=array('deleted'=>1);
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['member']=cfc('core')->member_show($v['mid']);
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'ddel','list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),				
		),
		'action'=>array(	
			array('type'=>'href','action'=>'/merch.order_detail/','title'=>'订单详情'),
			
		),),
		
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='print' || $_RQ['op']=='print4' || $_RQ['op']=='print5'){
	$order_id=intval($_RQ['id']);
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id),array('ispay'));
	$print_type=array(1,2);
	if($_RQ['op']=='print4'){
		$print_type=4;
	}elseif($_RQ['op']=='print5'){
		$print_type=5;
	}
	$res=model('jy_weishop/printmsg')->storeprint('',$order_id,'','','','','','',$print_type);
	if($res){
		exi('打印成功');
	}else{
		exi('打印失败,该订单所属门店没有添加打印任务！','error');
	}
}
if($_RQ['op']=='upprice'){
	$id=intval($_RQ['id']);
	$post=get_postdata();	
	if($post['price']){
		if(!is_numeric($post['price'])){
			exi('价格必须是数字！','error');
		}
		if(!method_exists(cfc('pay'),'change_pay_fee')){
			exi('修改价格失败！请联系管理员，更新框架到最新','error');
		}
		$price=floatval($post['price']);
		if ($price < 0) 
		{
			$price = 0;
		}
		model('jy_weishop/order')->upprice($post['id'],$price);
		exi('改价成功！','','jy_newpaimerch/merch.order_list/');	
	}
	$item=pdo_get('jy_weishop_order',array('id'=>$id),array('id','price'));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改价格',
	);
	$htmls['edit']=array(
		array('type'=>'string','title'=>'价格','name'=>'price','help'=>'修改价格！'),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_update('jy_weishop_order',array('deleted'=>1),array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_weishop_order',array('deleted'=>1),array('id'=>$id));
			}
		}
	}
	exi('删除成功！','','/merch.order_list/list/');
}
if($_RQ['op']=='ddel'){

	$id=intval($_RQ['id']);
	if($id){
		model('jy_merch/order')->order_delete($id);	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				model('jy_merch/order')->order_delete($id);
			}
		}
	}
	exi('删除成功！','','/merch.order_list/deleted');
}
if($_RQ['op']=='payyoufee'){
	$post=get_postdata();
	$orderid=$_RQ['orderid'];
	if(!$orderid){
		exi('请选择订单','error');
	}
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
	if($order['buytype']!=4){
		exi('非只付运费订单，不能操作！','error');
	}
	$merch_order=pdo_get('jy_merch_order',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$orderid));
	if($merch_order['isyoufee']){
		exi('该订单已支付运费！','error');
	}
	if($merch_order['payid']){
		$res=array(
			'pay_id'=>$merch_order['payid'],
			'order_id'=>$orderid,
		);
		exi($res);
	}else{
		$fee=$merch_order['sys_peiprice'];
		$params=array(
			'action'=>'payyoufee',
			'id'=>$merch_order['id'],
			'fee'=>$fee,
			'title'=>'商户支付运费',
			'plugin'=>'jy_newpaimerch',
			//'pay_type'=>'wechat',
			'no_type'=>array('hdfk'),
			'back_url'=>'jy_newpaimerch/merch.order_list/pay_result/',
			'success_url'=>'jy_newpaimerch/merch.order_list/',
			'error_url'=>'jy_newpaimerch/merch.order_list/',
		);
		$pay_id=cfc('pay')->create_pay_order($params);
		pdo_update('jy_merch_order',array('payid'=>$pay_id),array('id'=>$merch_order['id'],'uniacid'=>$_SESSION['uniacid']));
		$res=array(
			'pay_id'=>$pay_id,
			'order_id'=>$orderid,
		);
		exi($res);
	}
}
if($_RQ['op']=='pay_result'){
	$payresult=$_SESSION['pay_result'];
	$merch_order=pdo_get('jy_merch_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($merch_order['sys_peiprice']!=$payresult['fee']){
		//exi('支付失败，支付金额不匹配！','error');
	}
	if($recharge['isyoufee']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($payresult['status']!=1){
		exi('充值失败！','error');
	}
	pdo_update('jy_merch_order',array('isyoufee'=>1),array('id'=>$merch_order['id'],'uniacid'=>$_SESSION['uniacid']));
	pdo_update('jy_weishop_order',array('send_status'=>12),array('uniacid'=>$_SESSION['uniacid'],'id'=>$merch_order['order_id']));
	pdo_update('jy_weishop_order_goods',array('send_status'=>12),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$merch_order['order_id']));
	exi('支付成功！');
}
?>